% Copyright (C) 2009,2010,2011,2012  Marco Restelli
%
% This file is part of:
%   LDGH -- Local Hybridizable Discontinuous Galerkin toolkit
%
% LDGH is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% LDGH is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
% or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License
% along with LDGH. If not, see <http://www.gnu.org/licenses/>.
%
% author: Marco Restelli                   <marco.restelli@gmail.com>


function [Ux,gUx] = elem_interp(base,e,U,x)
% [Ux,gUx] = elem_interp(base,e,U,x)
%
% Element based interpolation: e is an element, U is a row vector with
% the modal degrees of freedom and x is the array of the points where
% the interpolated values are sought. The output are the function
% values Ux and the function gradients gUx.

 % Remap on the reference element
 xx = zeros(size(x));
 for i=1:e.d
   xx(i,:) = x(i,:) - e.x0(i);
 end
 xi = e.bi*xx;

 % evaluate the basis function and the gradients
 np = size(x,2);
 PHI  = zeros(    base.pk,np);
 gPHI = zeros(e.d,base.pk,np);
 for i=1:base.pk
   PHI(i,:) = ev_pol(base.p_s{i},xi);
   for j=1:e.d
     gPHI(j,i,:) = ev_pol(base.gradp_s{j,i},xi);
   end
 end
 for j=1:np
   gPHI(:,:,j) = e.bi' * gPHI(:,:,j);
 end
 
 Ux = U * PHI;
 gUx = zeros(e.d,np);
 for j=1:e.d
   gUx(j,:) = U * permute(gPHI(j,:,:),[2 3 1]);
 end

return
